/**
 * 
 */
package info.niwota.ispy;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * @author qiangli
 *
 */
public class Preferences {
	private static final String APP_RUNNING_STATUS = "settings.app.running.status";
	
	private static final String TOGGLE_WARN_SOUND = "toggle.warn.sound";
	
	private static final String TOGGLE_WARN_FLASH = "toggle.warn.flash";
	
	private static final String TOGGLE_WARN_VIBRATE = "toggle.warn.vibrate";
	
	private static final String TOGGLE_WARN_QUIET = "toggle.warn.quiet";

	private static final String MONITOR_INTERVAL = "monitor.interval";

	//private static final String MONITOR_CLEARLOG = "monitor.clear_log";
	
	public static int getMonitorInterval(Context ctx) {
		final int def = 0;
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ctx);
			return Integer.parseInt(pref.getString(MONITOR_INTERVAL, "0"));
		} catch (Exception e) {
		}
		return def;
	}
	
//	public static boolean isMonitorClearLog(Context ctx) {
//		final boolean defvalue = false;
//		try {
//			final SharedPreferences pref = PreferenceManager
//					.getDefaultSharedPreferences(ctx);
//			return pref.getBoolean(MONITOR_CLEARLOG, defvalue);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return defvalue;
//	}
	
	public static boolean isWarnFlash(Context ctx) {
		return isWarn(ctx, TOGGLE_WARN_FLASH, true);
	}
	
	public static boolean isWarnSound(Context ctx) {
		return isWarn(ctx, TOGGLE_WARN_SOUND, false);
	}
	
	public static boolean isWarnVibrate(Context ctx) {
		return isWarn(ctx, TOGGLE_WARN_VIBRATE, false);
	}
	
	public static boolean isWarnQuiet(Context ctx) {
		return isWarn(ctx, TOGGLE_WARN_QUIET, true);
	}
	
	private static boolean isWarn(Context ctx, final String key, final boolean defvalue) {
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(key, defvalue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defvalue;
	}
	
	public static boolean getRunningStatus(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(APP_RUNNING_STATUS, defvalue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defvalue;
	}
	
	public static void updateRunningStatus(Context ctx, boolean b) {
		try {
			final SharedPreferences p = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			p.edit().putBoolean(APP_RUNNING_STATUS, b).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
